<template>
  <div class="loading-component">
    <div class="loadingEight">
      <span></span>
    </div>
  </div>
</template>

<script  name="LoginPage">
import {getUserInfo} from '/src/api/index'
import { useRouter } from 'vue-router';
const router = useRouter();
import { useUserStore } from "/src/store/modules/user";
import Toast from 'vant/lib/toast';
import 'vant/es/toast/style';
import { Notify } from 'vant';
import 'vant/es/notify/style';

export default {
  name: '',
  setup() {
    const state = reactive({
      tab1value: '0',
    });
    return { state };
  },
  data() {
    return {
      userStore : useUserStore(),
      router: useRouter(),
      formData : {
        name: '',
        pwd: '',
      }
    };
  },
  async created(){
    // Notify({
    //   message: '自动登录中...',
    //   type: 'primary',
    //   background: '#1989fa',
    //   duration: 600,
    //   // color: '#ad0000',
    //   // background: '#ffe1e1',
    // });
    // Toast.loading({message:"自动登录中",duration: 600})


    var url = window.location.href;             //获取当前url

    // var dz_url = url.split('#')[0];                //获取#/之前的字符串
    // var dz_url = url.split('/')[0];                //url的字符串

    //找到id，测试时候注释
    //   var urlParams = url.split('?');
    //   var cs = urlParams.length > 1 ? urlParams[1] : ''; //获取?之后的参数字符串，如果不存在则为''

    var cs = url.split('?')[1];                //获取?之后的参数字符串
    console.log('cs1:',cs)
    var cs_arr = cs.split('&');                    //参数字符串分割为数组
    var cs={};
    for(var i=0;i<cs_arr.length;i++){         //遍历数组，拿到json对象
      cs[cs_arr[i].split('=')[0]] = cs_arr[i].split('=')[1]

    }
    console.log('cs1:',cs)

    //自定义id
    /*let cs = {}
    cs.id = '202114137';*/

    localStorage.setItem('openIdFlag', "noOpen")
    // 查询信息并转json
    let userStore = useUserStore()
    userStore.setUserLoginToken(cs.account)
    const res = await getUserInfo(cs.account);
    // if(200 !== res.response.value.code){
    //   // Toast.fail("自动登录失败")
    //   Notify({
    //     background: '#ee0a24',
    //     message: '自动登录失败' });
    // }
    //  //2024-1
    // console.log('getUserInfo',res)
    // console.log('getUserInfo',res.response)
    // console.log('getUserInfo',res.response.value)
    let info = res.response.value.extend
    console.log('AccountInfo',info)
    // console.log('userStore.getUserToken()',userStore.getUserToken)
    // console.log(info)
    // let userInfo = info.USER.replace(/\\/g,'')
    // let userJson = JSON.parse(userInfo)
    //存储
    // console.log('info.TYPE',info.TYPE)
    // userStore.setUserType(info.TYPE)
    // console.log(userStore.getUserInfo)
    // console.log(userStore.getUserType)
    // 根据返回结果跳转页面
    // userStore.setInfo(info,'stud')
    // this.userStore.setUserType(0)
    // this.router.push('/home');
    userStore.setUserType(info.TYPE)
    if(info.TYPE == 'stud'){
      userStore.setInfo(info.userInfo,info.TYPE)
      localStorage.setItem("userType", info.TYPE)
      //console.log(info.userInfo)
      // this.userStore.setUserType(0)
      this.router.push('/mainMenu');
    }else if(info.TYPE == 'tea'){
      // this.userStore.setUserType(1)
      localStorage.setItem("userType", info.TYPE)
      console.log('teacher', info);
      userStore.setInfo(info.userInfo,info.TYPE)
      this.router.push('/mainMenu_tea');
    }else {
      //身份信息错误
      alert('身份信息错误')
    }
  },
};
</script>

<style scoped>
.loading-component{
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}
.loadingEight{
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin: 0 auto;
  margin-top:400px;
  position: relative;
  border:5px solid lightgreen;
  -webkit-animation: turn 1s linear infinite;
  margin: 0;
}
.loadingEight span{
  display: inline-block;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: lightgreen;
  position: absolute;
  left: 50%;
  margin-top: -15px;
  margin-left: -15px;
  -webkit-animation: changeBgColor 2s linear infinite;
}
@-webkit-keyframes changeBgColor{
  0%{
    background: lightgreen;
  }
  100%{
    background: lightblue;
  }
}
@-webkit-keyframes turn{
  0%{
    -webkit-transform: rotate(0deg);
    border-color: lightgreen;
  }
  100%{
    -webkit-transform: rotate(360deg);
    border-color: lightblue;
  }
}
</style>
